//
//  LBXProFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 12/14/12.
//  Copyright (c) 2012 UIT. All rights reserved.
//

#include <iostream>
#include "LBXProFilter.h"

char xproFragStr[] = ""
"precision lowp float;  "
"uniform sampler2D texture; "
"varying vec2 vtexCoor; "
"varying vec4 vposition;"

"uniform float red[256];"
"uniform float green[256];"
"uniform float blue[256];"

"void main()    "
"{  "

"	vec4 textureColor = texture2D(texture,vtexCoor);"
"   vec4 result;"
"   result.a = textureColor.a;" 
"   result.r = red[int(textureColor.r*255.0)];"
"   result.g = green[int(textureColor.g*255.0)];"
"   result.b = blue[int(textureColor.b*255.0)];"
"	gl_FragColor = result;"
"}  ";

const float red[] = {0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000154, 0.002006, 0.004003, 0.006148, 0.008443, 0.010895, 0.013504, 0.016277, 0.019215, 0.022324, 0.025606, 0.029065, 0.032706, 0.036531, 0.040544, 0.044750, 0.049151, 0.054688, 0.059462, 0.064300, 0.069202, 0.074166, 0.079191, 0.084276, 0.089421, 0.094624, 0.099885, 0.105202, 0.110575, 0.116003, 0.121484, 0.127018, 0.132604, 0.138241, 0.143928, 0.149664, 0.155448, 0.161279, 0.167157, 0.173079, 0.179046, 0.185057, 0.191109, 0.197204, 0.203339, 0.209513, 0.215726, 0.221977, 0.228265, 0.234589, 0.240947, 0.247340, 0.253765, 0.260223, 0.266711, 0.273230, 0.279779, 0.286355, 0.292959, 0.299590, 0.306246, 0.312926, 0.319630, 0.326357, 0.333106, 0.339875, 0.346664, 0.353473, 0.360299, 0.367142, 0.374001, 0.380876, 0.387764, 0.394666, 0.401581, 0.408506, 0.415442, 0.422388, 0.429342, 0.436304, 0.443273, 0.450247, 0.457226, 0.464209, 0.471195, 0.478183, 0.485172, 0.492161, 0.499149, 0.506135, 0.513119, 0.520099, 0.527075, 0.534044, 0.541008, 0.547964, 0.554911, 0.561850, 0.568778, 0.575695, 0.582599, 0.589491, 0.596369, 0.603231, 0.610078, 0.616908, 0.623720, 0.630514, 0.637288, 0.644041, 0.650772, 0.657482, 0.664167, 0.670829, 0.677465, 0.684075, 0.690658, 0.697212, 0.703738, 0.710233, 0.716698, 0.723130, 0.729530, 0.735896, 0.742227, 0.748523, 0.754782, 0.761004, 0.767187, 0.773330, 0.779434, 0.785496, 0.791515, 0.797492, 0.803424, 0.809312, 0.815153, 0.820947, 0.826694, 0.832392, 0.838040, 0.843637, 0.849182, 0.854675, 0.860115, 0.865500, 0.870829, 0.876103, 0.881318, 0.886476, 0.891575, 0.896613, 0.901590, 0.906506, 0.911358, 0.916146, 0.917969, 0.922618, 0.927103, 0.931428, 0.935596, 0.939608, 0.943468, 0.947178, 0.950742, 0.954161, 0.957438, 0.960577, 0.963579, 0.966449, 0.969187, 0.971797, 0.974282, 0.976644, 0.978887, 0.981012, 0.983022, 0.984921, 0.986710, 0.988393, 0.989973, 0.991451, 0.992831, 0.994115, 0.995306, 0.996407, 0.997421, 0.998350, 0.999196, 0.999963, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000};

const float green[] = {0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000849, 0.001875, 0.002987, 0.004189, 0.005484, 0.006872, 0.008358, 0.009943, 0.011630, 0.013421, 0.015319, 0.017327, 0.019447, 0.021680, 0.024031, 0.026501, 0.029093, 0.031809, 0.034652, 0.037625, 0.040729, 0.043967, 0.047342, 0.050856, 0.054512, 0.058312, 0.062259, 0.066355, 0.070603, 0.075005, 0.079563, 0.082031, 0.086792, 0.091631, 0.096545, 0.101535, 0.106599, 0.111735, 0.116943, 0.122221, 0.127568, 0.132983, 0.138465, 0.144012, 0.149623, 0.155297, 0.161033, 0.166829, 0.172685, 0.178599, 0.184570, 0.190597, 0.196678, 0.202813, 0.209000, 0.215237, 0.221525, 0.227861, 0.234244, 0.240673, 0.247147, 0.253665, 0.260225, 0.266827, 0.273469, 0.280149, 0.286867, 0.293621, 0.300411, 0.307235, 0.314091, 0.320979, 0.327898, 0.334845, 0.341821, 0.348823, 0.355851, 0.362904, 0.369979, 0.377076, 0.384194, 0.391332, 0.398488, 0.405661, 0.412850, 0.420053, 0.427270, 0.434499, 0.441740, 0.448990, 0.456249, 0.463515, 0.470787, 0.478065, 0.485346, 0.492630, 0.499915, 0.507201, 0.514485, 0.521767, 0.529046, 0.536320, 0.543589, 0.550850, 0.558103, 0.565346, 0.572579, 0.579800, 0.587008, 0.594201, 0.601379, 0.608540, 0.615684, 0.622808, 0.629912, 0.636994, 0.644053, 0.651089, 0.658099, 0.665083, 0.672039, 0.678967, 0.685865, 0.692731, 0.699565, 0.706365, 0.713130, 0.719860, 0.726552, 0.733206, 0.739819, 0.746393, 0.752924, 0.759411, 0.765854, 0.772252, 0.778602, 0.784905, 0.791158, 0.797360, 0.803511, 0.809609, 0.815653, 0.821641, 0.827573, 0.833446, 0.839261, 0.845016, 0.850709, 0.856340, 0.861907, 0.867408, 0.872844, 0.878212, 0.883511, 0.888741, 0.893899, 0.898985, 0.903998, 0.908936, 0.914063, 0.918799, 0.923371, 0.927781, 0.932033, 0.936128, 0.940070, 0.943861, 0.947504, 0.951002, 0.954357, 0.957572, 0.960651, 0.963594, 0.966406, 0.969089, 0.971646, 0.974080, 0.976392, 0.978586, 0.980665, 0.982632, 0.984488, 0.986237, 0.987882, 0.989425, 0.990869, 0.992216, 0.993470, 0.994633, 0.995708, 0.996697, 0.997604, 0.998430, 0.999179, 0.999854, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000};

const float blue[] = {0.132813, 0.135600, 0.138388, 0.141176, 0.143964, 0.146752, 0.149540, 0.152328, 0.155116, 0.157904, 0.160692, 0.163480, 0.166268, 0.169056, 0.171844, 0.174632, 0.177420, 0.180208, 0.182996, 0.185784, 0.188572, 0.191360, 0.194148, 0.196936, 0.199724, 0.202512, 0.205300, 0.208088, 0.210876, 0.213664, 0.216452, 0.219240, 0.222028, 0.224816, 0.227604, 0.230392, 0.233180, 0.235968, 0.238756, 0.241544, 0.244332, 0.247120, 0.249908, 0.252696, 0.255484, 0.258272, 0.261060, 0.263848, 0.266636, 0.269424, 0.272212, 0.275000, 0.277788, 0.280576, 0.283364, 0.286152, 0.288940, 0.291728, 0.294516, 0.297304, 0.300092, 0.302880, 0.305668, 0.308456, 0.311244, 0.314032, 0.316820, 0.319608, 0.322396, 0.325184, 0.327972, 0.330760, 0.333548, 0.336336, 0.339124, 0.341912, 0.344700, 0.347488, 0.350276, 0.353064, 0.355852, 0.358640, 0.361428, 0.364216, 0.367004, 0.369792, 0.372580, 0.375368, 0.378156, 0.380944, 0.383732, 0.386520, 0.389308, 0.392096, 0.394884, 0.397672, 0.400460, 0.403248, 0.406036, 0.408824, 0.411612, 0.414400, 0.417187, 0.419975, 0.422763, 0.425551, 0.428339, 0.431127, 0.433915, 0.436703, 0.439491, 0.442279, 0.445067, 0.447855, 0.450643, 0.453431, 0.456219, 0.459007, 0.461795, 0.464583, 0.467371, 0.470159, 0.472947, 0.475735, 0.478523, 0.481311, 0.484099, 0.486887, 0.489675, 0.492463, 0.495251, 0.498039, 0.500827, 0.503615, 0.506403, 0.509191, 0.511979, 0.514767, 0.517555, 0.520343, 0.523131, 0.525919, 0.528707, 0.531495, 0.534283, 0.537071, 0.539859, 0.542647, 0.545435, 0.548223, 0.551011, 0.553799, 0.556587, 0.559375, 0.562163, 0.564951, 0.567739, 0.570527, 0.573315, 0.576103, 0.578891, 0.581679, 0.584467, 0.587255, 0.590043, 0.592831, 0.595619, 0.598407, 0.601195, 0.603983, 0.606771, 0.609559, 0.612347, 0.615135, 0.617923, 0.620711, 0.623499, 0.626287, 0.629075, 0.631863, 0.634651, 0.637439, 0.640227, 0.643015, 0.645803, 0.648591, 0.651379, 0.654167, 0.656955, 0.659743, 0.662531, 0.665319, 0.668107, 0.670895, 0.673683, 0.676471, 0.679259, 0.682047, 0.684835, 0.687623, 0.690411, 0.693199, 0.695987, 0.698775, 0.701563, 0.704350, 0.707138, 0.709926, 0.712714, 0.715502, 0.718290, 0.721078, 0.723866, 0.726654, 0.729442, 0.732230, 0.735018, 0.737806, 0.740594, 0.743382, 0.746170, 0.748958, 0.751746, 0.754534, 0.757322, 0.760110, 0.762898, 0.765686, 0.768474, 0.771262, 0.774050, 0.776838, 0.779626, 0.782414, 0.785202, 0.787990, 0.790778, 0.793566, 0.796354, 0.799142, 0.801930, 0.804718, 0.807506, 0.810294, 0.813082, 0.815870, 0.818658, 0.821446, 0.824234, 0.827022, 0.829810, 0.832598, 0.835386, 0.838174, 0.840962, 0.843750};


LBXProFilter::LBXProFilter(){
    init(0, xproFragStr);
    
    redUniform = program->uniformIndex("red");
    greenUniform = program->uniformIndex("green");
    blueUniform = program->uniformIndex("blue");
    
    glUniform1fv(redUniform, 256, red);
    glUniform1fv(greenUniform, 256, green);
    glUniform1fv(blueUniform, 256, blue);
}